import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: SlidablePage(),
    );
  }
}

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Home Page'),
      ),
      body: Center(
        child: MaterialButton(
          child: Text('Go to Details Page'),
          onPressed: () {

            Navigator.of(context).push(_createRoute());
            return;
            Navigator.of(context).push(MaterialPageRoute(builder: (context){
              return DetailsPage();
            }));
            return;
          },
        ),
      ),
    );
  }

  Route _createRoute() {
    return PageRouteBuilder(
        reverseTransitionDuration:Duration(seconds: 3),
      transitionDuration: Duration(seconds: 1),
      pageBuilder: (context, animation, secondaryAnimation) => DetailsPage(),
      transitionsBuilder: (context, animation, secondaryAnimation, child) {
        // 定义动画
        return SlideTransition(
          position: Tween<Offset>(
            begin: Offset(1.0, 0.0),
            end: Offset(0.0, 0.0),
          ).animate(animation),
          child: SlideTransition(
            position: Tween<Offset>(
              begin: Offset(0.0, 0.0),
              end: Offset(1.0, 0.0),
            ).animate(secondaryAnimation),
            child: child,
          ),
        );
      },
    );
  }
}

class SlidablePage extends StatefulWidget {
  @override
  _SlidablePageState createState() => _SlidablePageState();
}

class _SlidablePageState extends State<SlidablePage> {
  final _scaffoldKey = GlobalKey<ScaffoldState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _scaffoldKey,
      body: WillPopScope(
        onWillPop: () async {
          if (_scaffoldKey.currentState!.isDrawerOpen) {
            _scaffoldKey.currentState?.openEndDrawer();
            return false;
          }
          return true;
        },
        child: GestureDetector(
          onHorizontalDragStart: (details) {},
          onHorizontalDragUpdate: (details) {},
          onHorizontalDragEnd: (details) {
            if (details.primaryVelocity!.abs() > 300) {
              _scaffoldKey.currentState?.openEndDrawer();
            }
          },
          child: Center(
            child: Text('Swipe to open the end drawer'),
          ),
        ),
      ),
    );
  }
}
class DetailsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Details Page'),
      ),
      body: Container(
        color: Colors.red,
        child: Center(
          child: Text('Details page content goes here'),
        ),
      ),
    );
  }
}